package com.cwt_taxi_xa;

import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.json.JSONObject;

import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.constants.SPConstants;
import com.cwt_taxi_xa.data.SijiInfo;
import com.cwt_taxi_xa.protocol.MCUUtil;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.ExceptionUtil;
import com.cwt_taxi_xa.util.HttpUtils;
import com.cwt_taxi_xa.util.SPUtils;
import com.cwt_taxi_xa.util.StringParser;
import com.cwt_taxi_xa.util.ToastUtils;
import com.cwt_taxi_xa.util.Tools;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityInfo extends CWTBaseActivity{

	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {

			try {
				pd.dismiss();
			} catch (Exception e) {
			}

			if(msg.what==1001){
				String clientcode = SPUtils.getParamString(SPConstants.SPK_clientcode, SPConstants.SPV_clientcode);
				String carcode = SPUtils.getParamString(SPConstants.SPK_carcode, SPConstants.SPV_carcode);

				String mac =  Tools.getMacAddress(1);


				tv1.setText(clientcode);
				tv2.setText(carcode);
				tv3.setText(mac);
				Toast.makeText(ActivityInfo.this, "激活成功", Toast.LENGTH_SHORT).show();
			}
			if(msg.what==1002){
				Toast.makeText(ActivityInfo.this, "激活失败，请检查网络", Toast.LENGTH_LONG).show();
			}
			if(msg.what==1003){
				String str = (String) msg.obj;
				Toast.makeText(ActivityInfo.this,"激活失败："+ str, Toast.LENGTH_LONG).show();
			}
			if(msg.what==1004){
				tv4.setText((String)msg.obj);
			}
		};
	};
	ProgressDialog pd;
	TextView tv1, tv2, tv3, tv4;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_aa_sbinfo);
		tv1 = (TextView) findViewById(R.id.textView4);
		tv2 = (TextView) findViewById(R.id.textView5);
		tv3 = (TextView) findViewById(R.id.textView6);
		tv4 = (TextView) findViewById(R.id.textView8);

		MCUUtil.checkV();



	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		String clientcode = SPUtils.getParamString(SPConstants.SPK_clientcode, SPConstants.SPV_clientcode);
		String carcode = SPUtils.getParamString(SPConstants.SPK_carcode, SPConstants.SPV_carcode);

		String mac =  Tools.getMacAddress(1);


		tv1.setText(clientcode);
		tv2.setText(carcode);
		tv3.setText(mac);

		if(mac==null||mac.equals("")){
			tv3.setText("设备异常，联系：029-88222221");//
		}else{
			if(clientcode==null||clientcode.equals("")){
				if (pd == null) {
					pd = ProgressDialog.show(ActivityInfo.this, "请稍等", "正在激活");
				} else {
					pd.show();
				}
				doJiHuo(mac);
			}
		}


		MCUUtil.checkV();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		tv4.setText("");
	}


	/**
	 *
	 * @param context
	 * @return
	 */
	public  void doJiHuo(final String mac) {
		new AsyncTask<String, Void, Void>() {

			@Override
			protected Void doInBackground(String... params) {
				try {

					String url = null;
					url = "http://taxi.jpfwpt.com:4657/rest/getTermInfo/"+mac;
					HttpEntity httpEntity = HttpUtils.getEntity(url, null, HttpUtils.METHOD_POST);
					InputStream inputStream = HttpUtils.getStream(httpEntity);
					String json = StringParser.parse(inputStream);
					AppLog.e(json);
					JSONObject obj = new JSONObject(json);
					int status = obj.getInt("errorcode");
					if (status == 0) {
						obj = obj.getJSONObject("data");
						SPUtils.setParam(SPConstants.SPK_clientcode, obj.getString("termNum"));
						SPUtils.setParam(SPConstants.SPK_carcode, obj.getString("licNum"));
						handler.sendMessage(handler.obtainMessage(1001));
					} else {
						String message = obj.getString("message");
						AppLog.e(message);
						handler.sendMessage(handler.obtainMessage(1003, message));
					}

				}catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					AppLog.e(ExceptionUtil.getInfo(e), e);
					handler.sendEmptyMessage(1002);
				}
				return null;
			}
		}.execute();
	}





	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub
		if(type==1015){
			handler.sendMessage(handler.obtainMessage(1004, obj));
		}
	}

}
